﻿// CEditRollCallList.cpp: 实现文件
//

#include "pch.h"
#include "RandomRollCall.h"
#include "CEditRollCallListDlg.h"
#include"CAddListNameDlg.h"


// CEditRollCallList 对话框

IMPLEMENT_DYNAMIC(CEditRollCallListDlg, CDialogEx)

CEditRollCallListDlg::CEditRollCallListDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG1, pParent)
	, m_checkSelectAll(FALSE)
	, m_checkInverseSelect(FALSE)
{
	//_callList = new Manager::CallListManger();
}

CEditRollCallListDlg::~CEditRollCallListDlg()
{
	//delete _callList;
}

void CEditRollCallListDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST1, m_callList);
	DDX_Check(pDX, IDC_CHECK1, m_checkSelectAll);
	DDX_Check(pDX, IDC_CHECK2, m_checkInverseSelect);
}


BEGIN_MESSAGE_MAP(CEditRollCallListDlg, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON1, &CEditRollCallListDlg::OnBnClickedButton1)
	ON_WM_GETMINMAXINFO()
	ON_BN_CLICKED(IDC_CHECK1, &CEditRollCallListDlg::OnBnClickedCheck1)
	ON_BN_CLICKED(IDC_CHECK2, &CEditRollCallListDlg::OnBnClickedCheck2)
	ON_BN_CLICKED(IDC_BUTTON2, &CEditRollCallListDlg::OnBnClickedButton2)
END_MESSAGE_MAP()


// CEditRollCallList 消息处理程序


BOOL CEditRollCallListDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	//UpdateData(TRUE);
	//Manager::ProgramPath path;
	//CString cstr;
	//CStdioFile file;
	HICON m_hIcon;
	m_hIcon = AfxGetApp()->LoadIconW(IDR_MAINFRAME);	//根据资源ID加载资源文件
	SetIcon(m_hIcon, FALSE);
	UpdatetListCtrl();

	//UpdateData(FALSE);
	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}

void CEditRollCallListDlg::UpdatetListCtrl()
{
	Manager::CallListManger callList;
	//CString path;
	//path = Manager::ProgramPath::Path();
	//path.ReleaseBuffer();
	//CString Path = Manager::ProgramPath::Path();

	//ReadFileByPath(Path, callList, cstr);
	callList.ReadFileByPath(Manager::ProgramPath::Path());
	//Manager::CallListData data = callList.list[3];
	//初始化列表控件
	m_callList.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_CHECKBOXES);	//整行选择、网格线、选择框
	m_callList.InsertColumn(0, _T("姓名"), LVCFMT_LEFT, 255); // 插入第1列的列名
	m_callList.InsertColumn(1, _T("学号"), LVCFMT_LEFT, 255); // 插入第2列的列名
															//m_callList.InsertColumn(2, _T("删除选中"), LVCFMT_LEFT, 110); // 插入第3列的列名
	m_callList.DeleteAllItems();
	int line = callList.list.GetCount();
	//插入行
	for (int i = 0; i < line; i++)
	{
		m_callList.InsertItem(i, L"");
		m_callList.SetItemText(i, 0, callList.list[i].name);	//设置第1列文本内容
		m_callList.SetItemText(i, 1, callList.list[i].no_);		//设置第2列文本内容
																//m_callList.SetItemText
	}

	//UpdataListCtrl(callList);
}

//void CEditRollCallListDlg::UpdataListCtrl(Manager::CallListManger& callList)
//{
//	m_callList.DeleteAllItems();
//	int line = callList.list.GetCount();
//	//插入行
//	for (int i = 0; i<line; i++)
//	{
//		m_callList.InsertItem(i, L"");
//		m_callList.SetItemText(i, 0, callList.list[i].name);	//设置第1列文本内容
//		m_callList.SetItemText(i, 1, callList.list[i].no_);		//设置第2列文本内容
//																//m_callList.SetItemText
//	}
//}

//void ReadFileByPath(CString& Path, Manager::CallListManger& callList, CString& cstr)
//{
//	CFile file;
//	if (file.Open(Path + FILECALLLISTNAME, CFile::modeRead | CFile::typeBinary, NULL))
//	{
//		BYTE head[3];
//		file.Read(head, 3);
//		//如果没指向文件开头就重新指向文件开头
//		if (!(head[0] == 0xEF && head[1] == 0xBB && head[2] == 0xBF))
//		{
//			file.SeekToBegin();
//		}
//
//		DWORD const len = file.GetLength();
//		char* byteBuf = new char[len + 1];
//		byteBuf[len] = '\0';
//		file.Read(byteBuf, len);
//		//关闭文件
//		file.Close();
//		callList.ByteTransformToCString(byteBuf, len, cstr);
//		//while(file.Read(strDataText));
//		callList.getList(cstr);
//
//	}
//}






void CEditRollCallListDlg::OnBnClickedButton1()
{
	CAddListNameDlg dlgAdd;
	if (dlgAdd.DoModal()==IDOK)
	{
		UpdatetListCtrl();
	}
	// TODO: 在此添加控件通知处理程序代码
}

void CEditRollCallListDlg::OnGetMinMaxInfo(MINMAXINFO* lpMMI)
{
	CPoint pt(560, 620); //定义宽和高  
	lpMMI->ptMinTrackSize = pt; //限制最小宽和高  
	CDialog::OnGetMinMaxInfo(lpMMI);
}




void CEditRollCallListDlg::OnBnClickedCheck1()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	if (m_checkSelectAll)
	{
		m_checkInverseSelect = FALSE;
	}
	int len = m_callList.GetItemCount();
	for (int i = 0; i < len; i++)
	{
		m_callList.SetCheck(i, m_checkSelectAll);
	}
	UpdateData(FALSE);
}


void CEditRollCallListDlg::OnBnClickedCheck2()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	if (m_checkInverseSelect)
	{
		m_checkSelectAll = FALSE;
	}
	for (int i = 0; i < m_callList.GetItemCount(); i++)
	{
		m_callList.SetCheck(i, !m_callList.GetCheck(i));
	}
	UpdateData(FALSE);
}


void CEditRollCallListDlg::OnBnClickedButton2()
{
	// TODO: 在此添加控件通知处理程序代码
	if (MessageBox(_T("确定删除选中名单项吗？"),_T("提示"),MB_YESNO)==IDNO)
	{
		return;
	}
	Manager::CallListManger listData;
	listData.ReadFileByPath(Manager::ProgramPath::Path());
	//int n=listData.list.GetUpperBound();
	for (int i = m_callList.GetItemCount()-1; i>=0 ; i--)
	{
		if (m_callList.GetCheck(i))
		{
			listData.list.RemoveAt(i);
		}
	}
	//auto t1 = listData.list[0];
	//auto t2 = listData.list[1];
	//auto t3 = listData.list[2];
	listData.WriteFileByPath(Manager::ProgramPath::Path());
	this->UpdatetListCtrl();
}
